package com.pmcc.core.web;

import com.pmcc.core.entity.security.Dict;
import com.pmcc.core.repository.DictRepository;
import com.pmcc.core.service.AuthorityService;
import com.pmcc.core.service.DictService;
import com.pmcc.core.service.EmployeeService;
//import com.pmcc.core.service.UserService;
//import com.pmcc.utils.DictVo;
//import com.pmcc.utils.ResultBean;
import com.pmcc.utils.BeanUtils;
import com.pmcc.utils.AntdPage;
import com.pmcc.utils.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典管理
 * Created by chenqf on 2017/8/18.
 */
@RestController()
@RequestMapping("api/v1/dict")
@Api(value = "DictController", description = "字典相关api")
public class DictController {

    @Autowired
    AuthorityService authorityService;

    @Autowired
    DictService dictService;

    @Autowired
    DictRepository dictRepository;

    @Autowired
    EmployeeService employeeService;

    /**
     * 查询
     *
     * @return
     */
    @ApiOperation(value="获取字典列表", notes="")
    @GetMapping(value="queryByPage")
    public ResultBean list(AntdPage antdPage, Dict dict) {
        return dictService.queryList(antdPage,dict);
    }

    @ApiOperation(value="根据ID查询", notes="")
    @GetMapping("queryById")
    public ResultBean queryById(String id) {
        Map map = new HashMap();
        ResultBean resultBean = new ResultBean();
        Dict dict = dictRepository.findById(id);
        List<Dict> dictList = dictRepository.findByParentIdAndDelFlagOrderBySortAsc(id, "0");
        for (Dict d: dictList) {
            d.setKey(d.getId());
        }
        map.put("dict",dict);
        map.put("detail",dictList);
        resultBean.setObject(map);
        resultBean.setResultCode(ResultBean.SUCCESS);
        return resultBean;
    }

    /**
     * 新增方法
     * @param dict
     * @return
     */
    @ApiOperation(value = "新增字典", notes = "", response = ResultBean.class)
    @PostMapping("")
    public ResultBean add(@RequestBody Dict dict, HttpServletRequest request) {
        ResultBean resultBean = new ResultBean();
        //处理默认信息
        dict.setDelFlag("0");
        dict.setParentId("root");
        dict.setStatus("Y");
        //排序
        List<Dict> dictList = dictRepository.findByParentIdAndDelFlagOrderBySortAsc("root", "0");
        if(dictList.size()==0){
            dict.setSort(1);
        }else{
            dict.setSort(dictList.get(dictList.size()-1).getSort()+1);
        }
        //保存
        try {
            //保存主信息
            dictRepository.saveAndFlush(dict);
            //保存明细信息
            dictService.saveDetail(dict.getId(),dict.getDetail());

            resultBean.setResultCode(ResultBean.SUCCESS);
        } catch (Exception e) {
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    /**
     * 修改方法
     * @param map
     * @return
     */
    @ApiOperation(value = "修改字典", notes = "", response = ResultBean.class)
    @PostMapping(value ="edit")
    public ResultBean edit(@RequestBody Map map) {
        ResultBean resultBean = new ResultBean();
        try {
            Dict dictOld = dictRepository.findById(map.get("id").toString());
            BeanUtils.setBeanData(dictOld,map);
            dictRepository.saveAndFlush(dictOld);
            //保存明细信息
            dictService.saveDetail(dictOld.getId(),(List<Map<String,String>>) map.get("detail"));
            resultBean.setResultCode(ResultBean.SUCCESS);
        } catch (Exception e) {
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除", notes = "", response = ResultBean.class)
    @GetMapping(value ="delete")
    public ResultBean delete(@ApiParam(required = true, name = "主表id", value = "id")String id) {
        return dictService.delete(id);
    }




    /**
     * 查询下拉框
     * @param code 字典类型编码
     * @return
     */
    @GetMapping(value = "queryCombo")
    @ResponseBody
    public List<Dict> queryCombo(String code) {
        return dictService.queryToCombo(code);
    }


    /**
     * 根据类型和值 转name
     *
     * @param code 字典类型编码
     * @param value 字典值
     * @return
     */
    @GetMapping("queryNameByCodeAndValue")
    @ResponseBody
    public String queryNameByCodeAndValue(String code, String value) {
        return dictService.queryDictName(code,value);
    }

}
